<?php

class Backoffice_Model_FormGroupFlipper extends CC_Backoffice_Form
{
    /** @var Doctrine\ORM\EntityManager */
    protected $_em;
    
    /**
     * Overrides the constructor
     * 
     * @param Doctrine\ORM\EntityManager $em
     * @param array $options
     */
    public function __construct(Doctrine\ORM\EntityManager $em, $options = null)
    {
        $this->setAttrib('id', 'groups-flippers');
        parent::__construct($options);
        $this->_em = $em;
        
        $dql = $this->_em->createQuery('SELECT f FROM CC\Entity\Flag f ORDER BY f.name ASC');
        $flags = $dql->getResult();
        
        // Iterate through flags
        foreach($flags as $flag)
        {
            $displayGroup = array();
            $privileges = $flag->getPrivileges();
            
            // Check if the flag has privileges
            if(count($privileges))
            {
                // Create a checkbox for each privilege and add them to a display group
                foreach($flag->getPrivileges() as $privilege)
                {
                    $checkbox = new Zend_Form_Element_Checkbox('flipper_'.$flag->getId().'_'.$privilege->getId());
                    $checkbox->setOptions(
                        array (
                        	'label' => '/'.$flag->getName().'/'.$privilege->getName().'/ ('.$privilege->getDescription().')'
                    	)
                    );
                    
                    $this->addElement($checkbox);
                    $displayGroup[] = $checkbox->getName();
                }
                
                // Set the display group title and add it
                $displayGroupTitle = ucfirst($flag->getName()).' ('.$flag->getDescription().')';
                $this->addDisplayGroup($displayGroup, $flag->getName(), array('legend' => $displayGroupTitle));
            }
        }
        
        // Add the group id
        $groupId = new Zend_Form_Element_Hidden('groupId');
        $groupId->setOptions(
            array(
                'validators' => array(
                    new Zend_Validate_Regex('/^\d*$/')
                )
            )
        );
        $this->addElement($groupId);
        
        // Add the submit button
        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Save permissions')
               ->setRequired(true);
               
        $this->addElement($submit);
    }
    
    /**
     * Overrides populate() in Zend_Form
     * 
     * @param array $data 
     * @access public
     * @return void
     */
    public function populate($data, $id)
    {
        $parsed = array('group_id' => $id);
        
        foreach($data as $flagId => $privilege)
        {
            foreach($privilege as $privilegeId => $allow)
            {
                $parsed['flipper_'.$flagId.'_'.$privilegeId] = $allow;
            }
        }
        
        parent::populate($parsed);
    }
    
    /**
     * Overrides getValues() in Zend_Form
     * 
     * @access public
     * @return array
     */
    public function getValues()
    {
        $raw = parent::getValues();
        
        $values = array(
            'groupId' => $raw['groupId'],
            'flipper' => array()
        );
        
        foreach ($raw as $key => $value) 
        {
            if (preg_match('/^flipper_([0-9]{1,})_([0-9]{1,})$/', $key)) 
            {
                $parts = explode('_', $key);
                if (!isset($values['flipper'][$parts[1]])) 
                {
                    $values['flipper'][$parts[1]] = array();
                }
                
                $values['flipper'][$parts[1]][$parts[2]] = $value;
            }
        }
        
        return $values;
    }
}

?>